<?php



@session_start();



extract($_REQUEST);



require_once "../lib/config.php";

require_once "../lib/functions.php";

require_once "../lib/pdomanager.php";

require_once "../lib/paginate_class.php";



$pdodbobj=new DB();

$pdodbobj->dbname=DBNAME;

$pdodbobj->hostname=HOST;

$pdodbobj->dbusername=USERNAME;

$pdodbobj->dbpassword=PASSWORD;

$pdodbobj->dbtype=TYPE;

$pdodbobj->portnumber=PORTNUMBER;



$starcount=1;



$connstatus=$pdodbobj->connect();



if(isset($_SESSION['Allyfolio_User_ID']) && trim($_SESSION['Allyfolio_User_ID'])>0)

{



    if($action=="friendrequest" && $touserid>0 )

    {

        $pdodbobj->insertData("af_user_friends",array("User_ID"=>$_SESSION['Allyfolio_User_ID'],"User_Friend_ID"=>$touserid,"Status"=>3,"requestdate"=>time()));

        if($pdodbobj->affectedrows>0)

        {

            echo "success";

        }    

    }

    if($action=="changereqstatus" && isset($status) )

    {

        $pdodbobj->updateData("af_user_friends",array("status"=>$status),array("Unique_User_Friend_ID"=>$recordid));

        $selectfriend=$pdodbobj->fetch_array($pdodbobj->query("select User_ID,User_Friend_ID,requestdate from af_user_friends where Unique_User_Friend_ID=:ufid order by Unique_User_Friend_ID desc",array("ufid"=>$recordid)));

        

        if($pdodbobj->affectedrows>0)

        {

            $tofriendid="";

            $fromfriendid="";

            if(count($selectfriend)>0)

            {

                $pdodbobj->insertData("af_user_friends",array("status"=>$status,"User_ID"=>$selectfriend[0]['User_Friend_ID'],"User_Friend_ID"=>$selectfriend[0]['User_ID'],"requestdate"=>$selectfriend[0]['requestdate']));

                $tofriendid=$selectfriend[0]['User_Friend_ID'];

                $fromfriendid=$selectfriend[0]['User_ID'];

            }

            

            $statusarr=array(1=>"Active_Friends_IDs",0=>"Blocked_Friends_IDs",3=>"",4=>"",5=>"");

            

            $selectuserinfo=$pdodbobj->fetch_array($pdodbobj->query("select Active_Friends_IDs,Blocked_Friends_IDs from af_users where User_ID=:ufid",array("ufid"=>$tofriendid)));

            

            $friendlist="";

            

            if($statusarr[$status]!="")

            {

                if($selectuserinfo[0][$statusarr[$status]]=="")

                {

                    $friendlist="'".$fromfriendid."'";

                }

                else

                {

                    $friendlist=$selectuserinfo[0][$statusarr[$status]].",'".$fromfriendid."'";

                }

                

                $pdodbobj->updateData("af_users",array($statusarr[$status]=>$friendlist),array("User_ID"=>$tofriendid));

                

            } 

            

            $selectuserinfo=$pdodbobj->fetch_array($pdodbobj->query("select Active_Friends_IDs,Blocked_Friends_IDs from af_users where User_ID=:ufid",array("ufid"=>$fromfriendid)));

            

            $friendlist="";

            

            if($statusarr[$status]!="")

            {

                if($selectuserinfo[0][$statusarr[$status]]=="")

                {

                    $friendlist="'".$tofriendid."'";

                }

                else

                {

                    $friendlist=$selectuserinfo[0][$statusarr[$status]].",'".$tofriendid."'";

                }

                

                $pdodbobj->updateData("af_users",array($statusarr[$status]=>$friendlist),array("User_ID"=>$fromfriendid));

                

            } 

            

            echo "success";

        }    

    }

    if($action=="blockunblocreqstatus" && isset($status) && isset($touserid) && $touserid>0 && isset($userrecordid) && $userrecordid>0 )
    {
        $statusarr=array(1=>"Active_Friends_IDs",0=>"Blocked_Friends_IDs",3=>"",4=>"",5=>"");
            
        $selectuserinfo=$pdodbobj->fetch_array($pdodbobj->query("select Active_Friends_IDs,Blocked_Friends_IDs from af_users where User_ID=:ufid",array("ufid"=>$userrecordid)));
        
        $friendlist="";
        $reverselist='';
        
        if($status==0 && count($selectuserinfo)>0)
        {
            $friendlist=trim(",".$selectuserinfo[0]['Active_Friends_IDs'].",");
            if(trim($selectuserinfo[0]['Blocked_Friends_IDs'])=='')
            {
                $reverselist="'".$touserid."'";    
            }
            else
            {
                $reverselist=trim($selectuserinfo[0]['Blocked_Friends_IDs']).",'".$touserid."'";
            }
        }
        //echo " friend list ".$friendlist." reverse : ".$reverselist;
        if($status==1 && count($selectuserinfo)>0)
        {
            $friendlist=trim(",".$selectuserinfo[0]['Blocked_Friends_IDs'].",");
            if(trim($selectuserinfo[0]['Active_Friends_IDs'])=='')
            {
                $reverselist="'".$touserid."'";    
            }
            else
            {
                $reverselist=trim($selectuserinfo[0]['Active_Friends_IDs']).",'".$touserid."'";
            }
        }
        //echo " friend list ".$friendlist." reverse : ".$reverselist;
        if(strstr($friendlist,",".$touserid.",") || strstr($friendlist,",'".$touserid."',"))
        {
            $friendlist=str_ireplace(",'".$touserid."',",",",$friendlist);
            $friendlist=str_ireplace(",".$touserid.",",",",$friendlist);
            
        }
        if($friendlist=="," || $friendlist==",,")
        {
            $friendlist='';
        }
        else
        {
            $friendlist=substr($friendlist,1,strlen($friendlist)-1);
            $friendlist=substr($friendlist,0,strlen($friendlist)-1);
        }
        //echo " friend list ".$friendlist." reverse : ".$reverselist;
        if($status==0 && count($selectuserinfo)>0)
        {
            
            $pdodbobj->updateData("af_users",array("Active_Friends_IDs"=>$friendlist,"Blocked_Friends_IDs"=>$reverselist),array("User_ID"=>$userrecordid));
            
            $pdodbobj->updateData("af_user_friends",array("Status"=>0),array("User_ID"=>$userrecordid,"User_Friend_ID"=>$touserid));
            $pdodbobj->updateData("af_user_friends",array("Status"=>0),array("User_ID"=>$touserid,"User_Friend_ID"=>$userrecordid));
            
            echo "success";
            exit;
        }
        
        if($status==1 && count($selectuserinfo)>0)
        {
            
            $pdodbobj->updateData("af_users",array("Active_Friends_IDs"=>$reverselist,"Blocked_Friends_IDs"=>$friendlist),array("User_ID"=>$userrecordid));
            $pdodbobj->updateData("af_user_friends",array("Status"=>1),array("User_ID"=>$userrecordid,"User_Friend_ID"=>$touserid));
            $pdodbobj->updateData("af_user_friends",array("Status"=>1),array("User_ID"=>$touserid,"User_Friend_ID"=>$userrecordid));
            
            echo "success";
        }
        
        
           
    }

}



?>